package com.aj.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.aj.utils.Contants;

public class EnterVerificationCodeActivity extends BaseActivity {

	public static EnterVerificationCodeActivity instance;

	private Button next;

	private String pageFlag = "";

	private Button resendbutton;

	private int time = 60;

	private Timer timer;

	private VerificationTimerTask verificationTimerTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_enter_verificationcode);
		initView();
	}

	private void initView() {
		instance = this;
		next = (Button) findViewById(R.id.next);
		resendbutton = (Button) findViewById(R.id.resendbutton);
		pageFlag = getIntent().getStringExtra(Contants.PAGEFLAG_STRING);
		if (pageFlag != null
				&& pageFlag.equals(Contants.FORGET_PAGEFLAG_STRING)) {
			setActionBarTitle(R.string.forget_title_text);
		} else {
			setActionBarTitle(R.string.register_title_text);
		}
		ClickEvent clickEvent = new ClickEvent();
		next.setOnClickListener(clickEvent);
		resendbutton.setOnClickListener(clickEvent);
	}

	class ClickEvent implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (v.getId()) {
			case R.id.next:
				intent = new Intent(Contants.SET_PASSWORD_ACTIVITY_ACTION);
				intent.putExtra(Contants.PAGEFLAG_STRING, pageFlag);
				startActivity(intent);
				break;
			case R.id.resendbutton:
				disableResendButton();
				initTask();
				break;
			default:
				break;
			}
		}
	}

	private void disableResendButton() {
		resendbutton.setClickable(false);
		resendbutton.setFocusable(false);
		resendbutton.setFocusableInTouchMode(false);
	}

	private void enableResendButton() {
		resendbutton.setClickable(true);
		resendbutton.setFocusable(true);
		resendbutton.setFocusableInTouchMode(true);
		resendbutton.requestFocus();
	}

	class VerificationTimerTask extends TimerTask {
		@Override
		public void run() {
			if (time <= 1) {
				handler.sendEmptyMessage(Contants.HANDLER_CANNELHANDLER_INT);
			} else {
				--time;
				handler.sendEmptyMessage(Contants.HANDLER_CHANGEUI_INT);
			}
		}
	}

	private void initTask() {
		if (verificationTimerTask == null) {
			verificationTimerTask = new VerificationTimerTask();
		}
		if (timer == null) {
			timer = new Timer();
		}
		timer.schedule(verificationTimerTask, 0, 1000);
	}

	private void cannelTask() {
		if (verificationTimerTask != null) {
			verificationTimerTask.cancel();
			verificationTimerTask = null;
		}
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		if (handler != null) {
			handler.removeMessages(Contants.HANDLER_CHANGEUI_INT);
		}
	}

	private Handler handler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case Contants.HANDLER_CHANGEUI_INT:
				resendbutton.setText(getString(R.string.resend_text) + "("
						+ time + ")");
				break;
			case Contants.HANDLER_CANNELHANDLER_INT:
				cannelTask();
				enableResendButton();
				break;
			default:
				break;
			}
			return true;
		}
	});

	@Override
	protected void onStop() {
		super.onStop();
		cannelTask();
	};
}
